import mimetypes
from datetime import datetime

import pandas as pd


class ExcelHandler(object):
    def __init__(self, file):
        self.file = file
        self.df = pd.read_excel(file, dtype={'代码':str})

    def transfer_tendency(self):
        selected_columns = ['代码', '    名称', '20日涨幅', '上市日期', '所属行业', '细分行业', '流通市值']
        df = self.df[selected_columns]
        df = df.rename(columns={'代码':'TICKER_CODE', '    名称':'TICKER_NAME', '20日涨幅':'DAY20_RISE',
                                '上市日期':'LIST_DATE', '所属行业':'LEVEL_NAME', '细分行业':'LEVEL_DETAIL',
                                '流通市值':'FLOAT_MARKET_CAP'})
        df = df[df['FLOAT_MARKET_CAP'] != '--']
        df = df[df['DAY20_RISE'] != '--']
        df['LIST_DATE'] = df['LIST_DATE'].apply(lambda item : datetime.strptime(str(item), '%Y%m%d'))
        df['FLOAT_MARKET_CAP'] = df['FLOAT_MARKET_CAP'].apply(lambda value : float(value/100000000))
        df['DAY20_RISE'] = df['DAY20_RISE'].apply(lambda value : float(value*100))
        current_time = datetime.now()
        df['UPDATE_TIME'] = df.apply(lambda row: current_time, axis=1)
        return df


class CsvHandler(object):
    def __init__(self, file):
        self.file = file
        self.df = pd.read_csv(file, encoding='gbk')

    def transfer_df(self):
        selected_columns = ['代码', '    名称', '20日涨幅', '上市日期', '所属行业', '细分行业', '流通市值']
        df = self.df[selected_columns]
        df = df.rename(columns={'代码':'TICKER_CODE', '    名称':'TICKER_NAME', '20日涨幅':'DAY20_RISE',
                                '上市日期':'LIST_DATE', '所属行业':'LEVEL_NAME', '细分行业':'LEVEL_DETAIL',
                                '流通市值':'FLOAT_MARKET_CAP'})
        df = df[df['FLOAT_MARKET_CAP'] != '--']
        df = df[df['DAY20_RISE'] != '--']
        df['LIST_DATE'] = df['LIST_DATE'].apply(lambda item : datetime.strptime(str(item), '%Y%m%d'))
        df['FLOAT_MARKET_CAP'] = df['FLOAT_MARKET_CAP'].apply(lambda value : float(int(value)/100000000))
        df['DAY20_RISE'] = df['DAY20_RISE'].apply(lambda value : float(value))
        current_time = datetime.now()
        df['UPDATE_TIME'] = df.apply(lambda row: current_time, axis=1)
        return df


if __name__ == '__main__':
    # eh =  ExcelHandler('C:\\Users\\Lazy\\Downloads\\export\\Table.xls')
    # print(eh.df)
    # kind, _ = mimetypes.guess_type('C:\\Users\\Lazy\\Downloads\\export\\Table.xls')
    # print(kind)
    # t = eh.transfer_tendency()
    # print(t)
    import re
    from io import StringIO

    contents = ''
    with open('C:\\Users\\Lazy\\Downloads\\export\\Table.txt', 'r', encoding='gbk') as infile, \
        open('C:\\Users\\Lazy\\Downloads\\export\\Table.csv', 'w', encoding='utf-8') as outfile:
        for line in infile:
            stripped_line = line.rstrip()
            if stripped_line:
                # 替换所有空格（含制表符）为逗号[8,10](@ref)
                # processed_line = re.sub(r' ', '', stripped_line)
                processed_line = re.sub(r'\t\t', ',', stripped_line)
                processed_line = re.sub(r'\t', ',', processed_line)
                processed_line = re.sub(r'\+', '', processed_line)
                processed_line = re.sub(r'%', '', processed_line)
                # print(processed_line)
                contents += processed_line + '\n'
                outfile.write(processed_line + '\n')

    pds = CsvHandler(StringIO(contents))
    # print(pds.df)
    print(pds.transfer_df())